

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.compression.quant.quant_utils &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.compression.quant.quant_utils</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.compression.quant.quant_utils</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;Quantization utils.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span>
<span class="kn">from</span> <span class="nn">...</span> <span class="kn">import</span> <span class="n">nn</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;load_nonquant_param_into_quant_net&quot;</span><span class="p">,</span> <span class="s2">&quot;query_quant_layers&quot;</span><span class="p">]</span>


<span class="k">def</span> <span class="nf">cal_quantization_params</span><span class="p">(</span><span class="n">input_min</span><span class="p">,</span>
                            <span class="n">input_max</span><span class="p">,</span>
                            <span class="n">quant_min</span><span class="p">,</span>
                            <span class="n">quant_max</span><span class="p">,</span>
                            <span class="n">data_type</span><span class="p">,</span>
                            <span class="n">symmetric</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculate quantization params for scale and zero point.</span>

<span class="sd">    Args:</span>
<span class="sd">        input_min (numpy.ndarray): The dimension of channel or 1.</span>
<span class="sd">        input_max (numpy.ndarray): The dimension of channel or 1.</span>
<span class="sd">        quant_min (int): The minimum quantization integer.</span>
<span class="sd">        quant_max (int): The maximum quantization integer.</span>
<span class="sd">        data_type (numpy type) : Can be numpy int8, numpy uint8.</span>
<span class="sd">        symmetric (bool): Whether the quantization algorithm is symmetric or not. Default: False.</span>

<span class="sd">    Returns:</span>
<span class="sd">        scale (numpy.ndarray): quantization param.</span>
<span class="sd">        zero point (numpy.ndarray): quantization param.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">input_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">input_max</span><span class="p">)</span>
    <span class="n">input_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">input_min</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">input_min</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">input_max</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;input min shape should be equal to input max.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">input_min</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;input min and max shape should be one dim.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">input_min</span> <span class="o">&gt;</span> <span class="n">input_max</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;input_min min should be less than input max.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">input_max</span> <span class="o">==</span> <span class="n">input_min</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">input_min</span><span class="o">.</span><span class="n">shape</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">input_min</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>

    <span class="c1"># calculate scale</span>
    <span class="k">if</span> <span class="n">symmetric</span><span class="p">:</span>
        <span class="n">input_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="o">-</span><span class="n">input_min</span><span class="p">,</span> <span class="n">input_max</span><span class="p">)</span>
        <span class="n">input_min</span> <span class="o">=</span> <span class="o">-</span><span class="n">input_max</span>
    <span class="n">scale</span> <span class="o">=</span> <span class="p">(</span><span class="n">input_max</span> <span class="o">-</span> <span class="n">input_min</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">quant_max</span> <span class="o">-</span> <span class="n">quant_min</span><span class="p">)</span>

    <span class="c1"># calculate zero point</span>
    <span class="k">if</span> <span class="n">data_type</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">int8</span> <span class="ow">and</span> <span class="n">symmetric</span><span class="p">:</span>
        <span class="n">zp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">input_min</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">zp_double</span> <span class="o">=</span> <span class="n">quant_min</span> <span class="o">-</span> <span class="n">input_min</span> <span class="o">/</span> <span class="n">scale</span>
        <span class="n">zp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">zp_double</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">scale</span><span class="p">,</span> <span class="n">zp</span>


<span class="k">def</span> <span class="nf">get_quant_min_max</span><span class="p">(</span><span class="n">data_type</span><span class="p">,</span> <span class="n">num_bits</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">narrow_range</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Calculate quantization params for minimum/maximum quantization integer&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">data_type</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">int8</span><span class="p">:</span>
        <span class="n">quant_min</span> <span class="o">=</span> <span class="mi">0</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">**</span> <span class="p">(</span><span class="n">num_bits</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">quant_max</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">**</span> <span class="p">(</span><span class="n">num_bits</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="k">elif</span> <span class="n">data_type</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">:</span>
        <span class="n">quant_min</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">quant_max</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">**</span> <span class="n">num_bits</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported datatype(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">data_type</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">narrow_range</span><span class="p">:</span>
        <span class="n">quant_min</span> <span class="o">=</span> <span class="n">quant_min</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">quant_min</span><span class="p">,</span> <span class="n">quant_max</span>


<span class="k">def</span> <span class="nf">weight2int</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">scale</span><span class="p">,</span> <span class="n">zero_point</span><span class="p">,</span> <span class="n">quant_min</span><span class="p">,</span> <span class="n">quant_max</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculate int8/uint8 weight from fp32. the formula is defined as:</span>

<span class="sd">    .. math::</span>
<span class="sd">        int8/uint8 = round(float/scale) + offset</span>

<span class="sd">    Args:</span>
<span class="sd">        data (numpy.ndarray): The dimension of channel or 1. Should be NCHW.</span>
<span class="sd">        scale (numpy.ndarray): The dimension of channel or 1.</span>
<span class="sd">        zero_point (numpy.ndarray): The dimension of channel or 1.</span>
<span class="sd">        quant_min (int): The minimum quantization integer.</span>
<span class="sd">        quant_max (int): The maximum quantization integer.</span>

<span class="sd">    Returns:</span>
<span class="sd">        weight (numpy.ndarray): The dimension of channel or 1.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">scale</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">zero_point</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;`scale` and `zero_point` should have the same shape.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">scale</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;`scale` and `zero_point` shape should be greater than zero.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">scale</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">scale</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="c1"># for perchannel</span>
        <span class="k">if</span> <span class="n">scale</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
            <span class="c1"># `Conv2d` or `Dense` op weight</span>
            <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
            <span class="n">scale</span> <span class="o">=</span> <span class="n">scale</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
            <span class="n">zero_point</span> <span class="o">=</span> <span class="n">zero_point</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">scale</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="c1"># `DepthwiseConv2d` op weight</span>
            <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
            <span class="n">scale</span> <span class="o">=</span> <span class="n">scale</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
            <span class="n">zero_point</span> <span class="o">=</span> <span class="n">zero_point</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported weight shape(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

    <span class="n">weight_int</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">((</span><span class="n">data</span> <span class="o">/</span> <span class="n">scale</span><span class="p">)</span> <span class="o">+</span> <span class="n">zero_point</span><span class="p">)</span>
    <span class="n">weight_int</span><span class="p">[</span><span class="n">weight_int</span> <span class="o">&gt;</span> <span class="n">quant_max</span><span class="p">]</span> <span class="o">=</span> <span class="n">quant_max</span>
    <span class="n">weight_int</span><span class="p">[</span><span class="n">weight_int</span> <span class="o">&lt;</span> <span class="n">quant_min</span><span class="p">]</span> <span class="o">=</span> <span class="n">quant_min</span>
    <span class="k">return</span> <span class="n">weight_int</span>


<span class="k">def</span> <span class="nf">scale_zp_max_min_from_fake_quant_cell</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">data_type</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Get calculate quantization params for scale, zero point, max and min from `FakeQuantWithMinMaxObserver`.&quot;&quot;&quot;</span>
    <span class="n">minq</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">minq</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">maxq</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">maxq</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="c1"># make sure maxq &gt; 0 and minq &lt;= 0</span>
    <span class="k">if</span> <span class="n">cell</span><span class="o">.</span><span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;LEARNED_SCALE&#39;</span><span class="p">:</span>
        <span class="n">maxq</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">maxq</span><span class="p">)</span>
        <span class="n">minq</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">minq</span><span class="p">)</span>
    <span class="n">quant_min</span><span class="p">,</span> <span class="n">quant_max</span> <span class="o">=</span> <span class="n">get_quant_min_max</span><span class="p">(</span><span class="n">data_type</span><span class="p">,</span> <span class="n">num_bits</span><span class="o">=</span><span class="n">cell</span><span class="o">.</span><span class="n">num_bits</span><span class="p">,</span> <span class="n">narrow_range</span><span class="o">=</span><span class="n">cell</span><span class="o">.</span><span class="n">narrow_range</span><span class="p">)</span>
    <span class="n">symmetric</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">symmetric</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cell</span><span class="o">.</span><span class="n">neg_trunc</span>
    <span class="n">scale</span><span class="p">,</span> <span class="n">zp</span> <span class="o">=</span> <span class="n">cal_quantization_params</span><span class="p">(</span>
        <span class="n">minq</span><span class="p">,</span> <span class="n">maxq</span><span class="p">,</span>
        <span class="n">quant_min</span><span class="p">,</span> <span class="n">quant_max</span><span class="p">,</span> <span class="n">data_type</span><span class="p">,</span>
        <span class="n">symmetric</span><span class="o">=</span><span class="n">symmetric</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">scale</span><span class="p">,</span> <span class="n">zp</span><span class="p">,</span> <span class="n">maxq</span><span class="p">,</span> <span class="n">minq</span>


<span class="k">def</span> <span class="nf">fold_batchnorm</span><span class="p">(</span><span class="n">weight</span><span class="p">,</span> <span class="n">cell_quant</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fold the batchnorm in `Conv2dBnFoldQuant` to weight.</span>

<span class="sd">    Calculate from `FakeQuantWithMinMax`&#39;s Parameter or Fake quant primitive.</span>

<span class="sd">    Args:</span>
<span class="sd">        weight (numpy.ndarray): Weight of `cell_quant`.</span>
<span class="sd">        cell_quant (Cell): Object of `mindspore.nn.layer.Conv2dBnFoldQuant`.</span>

<span class="sd">    Returns:</span>
<span class="sd">        weight (numpy.ndarray): Folded weight.</span>
<span class="sd">        bias (numpy.ndarray): Folded bias.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">variance</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">mean</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">moving_mean</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">gamma</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">beta</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">beta</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">epsilon</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">eps</span>
    <span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">variance</span> <span class="o">+</span> <span class="n">epsilon</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">gamma</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
        <span class="c1"># `Conv2d` or `Dense` op weight</span>
        <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="n">_gamma</span> <span class="o">=</span> <span class="n">gamma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="n">_sigma</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">gamma</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
        <span class="c1"># `DepthwiseConv2d` op weight</span>
        <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
        <span class="n">_gamma</span> <span class="o">=</span> <span class="n">gamma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="n">_sigma</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported weight shape(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

    <span class="n">weight</span> <span class="o">=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">_gamma</span> <span class="o">/</span> <span class="n">_sigma</span>
    <span class="n">bias</span> <span class="o">=</span> <span class="n">beta</span> <span class="o">-</span> <span class="n">gamma</span> <span class="o">*</span> <span class="n">mean</span> <span class="o">/</span> <span class="n">sigma</span>
    <span class="k">return</span> <span class="n">weight</span><span class="p">,</span> <span class="n">bias</span>


<span class="k">def</span> <span class="nf">without_fold_batchnorm</span><span class="p">(</span><span class="n">weight</span><span class="p">,</span> <span class="n">cell_quant</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fold the batchnorm in `Conv2dBnWithoutFoldQuant` to weight.</span>

<span class="sd">    Calculate from `FakeQuantWithMinMax`&#39;s Parameter or Fake quant primitive.</span>

<span class="sd">    Args:</span>
<span class="sd">        weight (numpy.ndarray): Weight of `cell_quant`.</span>
<span class="sd">        cell_quant (Cell): Object of `mindspore.nn.layer.Conv2dBnWithoutFoldQuant`.</span>

<span class="sd">    Returns:</span>
<span class="sd">        weight (numpy.ndarray): whihout folded weight.</span>
<span class="sd">        bias (numpy.ndarray): without folded bias.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">variance</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">mean</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_mean</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">gamma</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">beta</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">beta</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
    <span class="n">epsilon</span> <span class="o">=</span> <span class="n">cell_quant</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">eps</span>
    <span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">variance</span> <span class="o">+</span> <span class="n">epsilon</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">gamma</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
        <span class="c1"># `Conv2d` or `Dense` op weight</span>
        <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="n">_gamma</span> <span class="o">=</span> <span class="n">gamma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="n">_sigma</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">gamma</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
        <span class="c1"># `DepthwiseConv2d` op weight</span>
        <span class="n">shape_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
        <span class="n">_gamma</span> <span class="o">=</span> <span class="n">gamma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
        <span class="n">_sigma</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">shape_list</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported weight shape(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">weight</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

    <span class="n">weight</span> <span class="o">=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">_gamma</span> <span class="o">/</span> <span class="n">_sigma</span>
    <span class="n">bias</span> <span class="o">=</span> <span class="n">beta</span> <span class="o">-</span> <span class="n">gamma</span> <span class="o">*</span> <span class="n">mean</span> <span class="o">/</span> <span class="n">sigma</span>
    <span class="k">return</span> <span class="n">weight</span><span class="p">,</span> <span class="n">bias</span>


<span class="k">def</span> <span class="nf">compute_kl_threshold</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">bitwidth</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Using KL-J Distance to calculate the clip threshold.</span>

<span class="sd">    Args:</span>
<span class="sd">        - **data** (NumpyArray) - Data observed to calculate the threshold for quantization,</span>
<span class="sd">        - **bitwidth** (QuantDtype) - The datatype of quantization.</span>
<span class="sd">    Outputs:</span>
<span class="sd">        Tensor with Shape 1. Threshold to calculate the data.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">data_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">data_max</span> <span class="o">&lt;</span> <span class="mf">1e-5</span><span class="p">:</span>
        <span class="k">return</span> <span class="mf">1e-5</span>
    <span class="n">hist</span><span class="p">,</span> <span class="n">bin_edges</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">histogram</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">bins</span><span class="o">=</span><span class="s1">&#39;sqrt&#39;</span><span class="p">,</span> <span class="nb">range</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">data_max</span><span class="p">),</span> <span class="n">density</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="c1"># For the sake of high efficiency, we limit the maximum number of bins to 1024 in `sqrt` mode, If it exceeds the</span>
    <span class="c1"># largest size, turn to use the default bins config.</span>
    <span class="n">largest_bin_size</span> <span class="o">=</span> <span class="mi">1024</span>
    <span class="k">if</span> <span class="n">hist</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">largest_bin_size</span><span class="p">:</span>
        <span class="n">hist</span><span class="p">,</span> <span class="n">bin_edges</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">histogram</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="nb">range</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">data_max</span><span class="p">),</span> <span class="n">density</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">hist</span> <span class="o">=</span> <span class="n">hist</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">hist</span><span class="p">)</span>
    <span class="n">cumsum</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">hist</span><span class="p">)</span>
    <span class="n">bit_pow_range</span> <span class="o">=</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">bitwidth</span><span class="o">.</span><span class="n">num_bits</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">threshold</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">scaling_factor</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">kl</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">bit_pow_range</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">bin_edges</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">th_layer_out</span> <span class="o">=</span> <span class="n">bin_edges</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">th_layer_out</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bit_pow_range</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">bin_edges</span><span class="p">),</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">threshold_tmp</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">bin_edges</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">bin_edges</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">threshold</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">threshold</span><span class="p">,</span> <span class="p">[</span><span class="n">threshold_tmp</span><span class="p">]))</span>
        <span class="n">scaling_factor_tmp</span> <span class="o">=</span> <span class="n">threshold_tmp</span> <span class="o">/</span> <span class="p">(</span><span class="n">bit_pow_range</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">scaling_factor</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">scaling_factor</span><span class="p">,</span> <span class="p">[</span><span class="n">scaling_factor_tmp</span><span class="p">]))</span>
        <span class="c1"># forward interpolation</span>
        <span class="n">cumsum_tmp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">cumsum</span><span class="p">)</span>
        <span class="n">cumsum_tmp</span><span class="p">[(</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">fwd_x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">bit_pow_range</span><span class="p">)</span>
        <span class="n">fwd_xp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
        <span class="n">fwd_fp</span> <span class="o">=</span> <span class="n">cumsum_tmp</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span>
        <span class="n">forward_interp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">interp</span><span class="p">(</span><span class="n">fwd_x</span><span class="p">,</span> <span class="n">fwd_xp</span><span class="p">,</span> <span class="n">fwd_fp</span><span class="p">)</span>
        <span class="c1"># backward interpolation</span>
        <span class="n">bwd_x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
        <span class="n">bwd_xp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">bit_pow_range</span><span class="p">)</span>
        <span class="n">bwd_fp</span> <span class="o">=</span> <span class="n">forward_interp</span>
        <span class="n">backward_interp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">interp</span><span class="p">(</span><span class="n">bwd_x</span><span class="p">,</span> <span class="n">bwd_xp</span><span class="p">,</span> <span class="n">bwd_fp</span><span class="p">)</span>
        <span class="n">cumsum_tmp</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">backward_interp</span>
        <span class="n">kl_tmp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">((</span><span class="n">cumsum</span> <span class="o">-</span> <span class="n">cumsum_tmp</span><span class="p">)</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log2</span><span class="p">(</span><span class="n">cumsum</span> <span class="o">/</span> <span class="n">cumsum_tmp</span><span class="p">))</span>  <span class="c1"># Kullback-Leibler-J</span>
        <span class="n">kl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">kl</span><span class="p">,</span> <span class="p">[</span><span class="n">kl_tmp</span><span class="p">]))</span>
    <span class="n">th_layer_out</span> <span class="o">=</span> <span class="n">threshold</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">kl</span><span class="p">)]</span>
    <span class="n">threshold</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">th_layer_out</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">threshold</span> <span class="o">&lt;</span> <span class="mf">1e-5</span><span class="p">:</span>
        <span class="n">threshold</span> <span class="o">=</span> <span class="mf">1e-5</span>
    <span class="k">return</span> <span class="n">threshold</span>


<div class="viewcode-block" id="query_quant_layers"><a class="viewcode-back" href="../../../../api_python/mindspore.compression.html#mindspore.compression.quant.query_quant_layers">[docs]</a><span class="k">def</span> <span class="nf">query_quant_layers</span><span class="p">(</span><span class="n">network</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Query the network&#39;s quantization strategy of each quantized layer and print it to the screen, note that all the</span>
<span class="sd">    quantization layers are queried before graph compile optimization in the graph mode, thus, some redundant quantized</span>
<span class="sd">    layers, which not exist in practical execution, may appear.</span>

<span class="sd">    Args:</span>
<span class="sd">        network (Cell): input network</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.compression.quant import QuantizationAwareTraining</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.compression.quant.quant_utils import query_quant_layers</span>
<span class="sd">        &gt;&gt;&gt; class LeNet5(nn.Cell):</span>
<span class="sd">        ...     def __init__(self, num_class=10, channel=1):</span>
<span class="sd">        ...         super(LeNet5, self).__init__()</span>
<span class="sd">        ...         self.type = &quot;fusion&quot;</span>
<span class="sd">        ...         self.num_class = num_class</span>
<span class="sd">        ...</span>
<span class="sd">        ...         # change `nn.Conv2d` to `nn.Conv2dBnAct`</span>
<span class="sd">        ...         self.conv1 = nn.Conv2dBnAct(channel, 6, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.conv2 = nn.Conv2dBnAct(6, 16, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         # change `nn.Dense` to `nn.DenseBnAct`</span>
<span class="sd">        ...         self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc2 = nn.DenseBnAct(120, 84, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc3 = nn.DenseBnAct(84, self.num_class)</span>
<span class="sd">        ...</span>
<span class="sd">        ...         self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)</span>
<span class="sd">        ...         self.flatten = nn.Flatten()</span>
<span class="sd">        ...</span>
<span class="sd">        ...     def construct(self, x):</span>
<span class="sd">        ...         x = self.conv1(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.conv2(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.flatten(x)</span>
<span class="sd">        ...         x = self.fc1(x)</span>
<span class="sd">        ...         x = self.fc2(x)</span>
<span class="sd">        ...         x = self.fc3(x)</span>
<span class="sd">        ...         return x</span>
<span class="sd">        ...</span>
<span class="sd">        &gt;&gt;&gt; net = LeNet5()</span>
<span class="sd">        &gt;&gt;&gt; quantizer = QuantizationAwareTraining(bn_fold=False, per_channel=[True, False], symmetric=[True, False])</span>
<span class="sd">        &gt;&gt;&gt; net_qat = quantizer.quantize(net)</span>
<span class="sd">        &gt;&gt;&gt; query_quant_layers(net_qat)</span>
<span class="sd">        conv1.conv.fake_quant_weight                                       INT8</span>
<span class="sd">        conv1.activation.fake_quant_act                                    INT8</span>
<span class="sd">        conv2.conv.fake_quant_weight                                       INT8</span>
<span class="sd">        conv2.activation.fake_quant_act                                    INT8</span>
<span class="sd">        fc1.dense.fake_quant_weight                                        INT8</span>
<span class="sd">        fc1.activation.fake_quant_act                                      INT8</span>
<span class="sd">        fc2.dense.fake_quant_weight                                        INT8</span>
<span class="sd">        fc2.activation.fake_quant_act                                      INT8</span>
<span class="sd">        fc3.dense.fake_quant_weight                                        INT8</span>
<span class="sd">        fc3.activation.fake_quant_act                                      INT8</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">network</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_isinstance</span><span class="p">(</span><span class="s2">&quot;network&quot;</span><span class="p">,</span> <span class="n">network</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">)</span>
    <span class="n">tplt</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{0:60}</span><span class="se">\t</span><span class="si">{1:10}</span><span class="s2">&quot;</span>
    <span class="k">for</span> <span class="n">cell_and_name</span> <span class="ow">in</span> <span class="n">network</span><span class="o">.</span><span class="n">cells_and_names</span><span class="p">():</span>
        <span class="n">cell_name</span> <span class="o">=</span> <span class="n">cell_and_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cell</span> <span class="o">=</span> <span class="n">cell_and_name</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">):</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">tplt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">cell_name</span><span class="p">,</span> <span class="n">cell</span><span class="o">.</span><span class="n">quant_dtype</span><span class="p">))</span></div>


<div class="viewcode-block" id="load_nonquant_param_into_quant_net"><a class="viewcode-back" href="../../../../api_python/mindspore.compression.html#mindspore.compression.quant.load_nonquant_param_into_quant_net">[docs]</a><span class="k">def</span> <span class="nf">load_nonquant_param_into_quant_net</span><span class="p">(</span><span class="n">quant_model</span><span class="p">,</span> <span class="n">params_dict</span><span class="p">,</span> <span class="n">quant_new_params</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Load fp32 model parameters into quantization model.</span>

<span class="sd">    Args:</span>
<span class="sd">        quant_model(Cell): Quantization model.</span>
<span class="sd">        params_dict(dict): Parameter dict that stores fp32 parameters.</span>
<span class="sd">        quant_new_params(list): Parameters that exist in quantization network but not in non-quantization</span>
<span class="sd">            network. Default: None.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `quant_new_params` is not None and is not list.</span>
<span class="sd">        ValueError: If there are parameters in the `quant_model` that are neither in `params_dict`</span>
<span class="sd">            nor in `quant_new_params`.</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import load_checkpoint</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.compression.quant.quant_utils import load_nonquant_param_into_quant_net</span>
<span class="sd">        &gt;&gt;&gt; class LeNet5(nn.Cell):</span>
<span class="sd">        ...     def __init__(self, num_class=10, channel=1):</span>
<span class="sd">        ...         super(LeNet5, self).__init__()</span>
<span class="sd">        ...         self.type = &quot;fusion&quot;</span>
<span class="sd">        ...         self.num_class = num_class</span>
<span class="sd">        ...</span>
<span class="sd">        ...         # change `nn.Conv2d` to `nn.Conv2dBnAct`</span>
<span class="sd">        ...         self.conv1 = nn.Conv2dBnAct(channel, 6, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.conv2 = nn.Conv2dBnAct(6, 16, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         # change `nn.Dense` to `nn.DenseBnAct`</span>
<span class="sd">        ...         self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc2 = nn.DenseBnAct(120, 84, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc3 = nn.DenseBnAct(84, self.num_class)</span>
<span class="sd">        ...</span>
<span class="sd">        ...         self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)</span>
<span class="sd">        ...         self.flatten = nn.Flatten()</span>
<span class="sd">        ...</span>
<span class="sd">        ...     def construct(self, x):</span>
<span class="sd">        ...         x = self.conv1(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.conv2(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.flatten(x)</span>
<span class="sd">        ...         x = self.fc1(x)</span>
<span class="sd">        ...         x = self.fc2(x)</span>
<span class="sd">        ...         x = self.fc3(x)</span>
<span class="sd">        ...         return x</span>
<span class="sd">        ...</span>
<span class="sd">        &gt;&gt;&gt; net = LeNet5()</span>
<span class="sd">        &gt;&gt;&gt; ckpt_file_name = &quot;./checkpoint/LeNet5_noquant-1_32.ckpt&quot;</span>
<span class="sd">        &gt;&gt;&gt; param_dict = load_checkpoint(ckpt_file_name)</span>
<span class="sd">        &gt;&gt;&gt; load_nonquant_param_into_quant_net(net, param_dict)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">quant_new_params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">quant_new_params</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;quant_new_params must be list or None.&quot;</span><span class="p">)</span>
    <span class="n">iterable_dict</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;minq&#39;</span><span class="p">:</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">item</span><span class="p">:</span> <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;minq&#39;</span><span class="p">),</span> <span class="n">params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()))),</span>
        <span class="s1">&#39;maxq&#39;</span><span class="p">:</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">item</span><span class="p">:</span> <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;maxq&#39;</span><span class="p">),</span> <span class="n">params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()))),</span>
        <span class="s1">&#39;quant_max&#39;</span><span class="p">:</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">item</span><span class="p">:</span> <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;quant_max&#39;</span><span class="p">),</span> <span class="n">params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">())))</span>
    <span class="p">}</span>
    <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="n">key_name</span> <span class="o">=</span> <span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">key_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">iterable_dict</span><span class="p">:</span>
            <span class="n">iterable_dict</span><span class="p">[</span><span class="n">key_name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">item</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">key_name</span><span class="p">:</span> <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">value</span><span class="p">),</span>
                                                       <span class="n">params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">())))</span>

    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">quant_model</span><span class="o">.</span><span class="n">parameters_and_names</span><span class="p">():</span>
        <span class="n">key_name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">key_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">iterable_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">key_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">quant_new_params</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Can&#39;t find match parameter in ckpt, param name = </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">continue</span>
        <span class="n">value_param</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">iterable_dict</span><span class="p">[</span><span class="n">key_name</span><span class="p">],</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value_param</span><span class="p">:</span>
            <span class="n">param</span><span class="o">.</span><span class="n">set_data</span><span class="p">(</span><span class="n">value_param</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;init model param </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s1"> with checkpoint param </span><span class="si">{</span><span class="n">value_param</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>


    <span class="c1"># Perform KL_init when learned scale quantization is executed.</span>
    <span class="k">for</span> <span class="n">cell_and_name</span> <span class="ow">in</span> <span class="n">quant_model</span><span class="o">.</span><span class="n">cells_and_names</span><span class="p">():</span>
        <span class="n">cell</span> <span class="o">=</span> <span class="n">cell_and_name</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnFoldQuantOneConv</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnFoldQuant</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnWithoutFoldQuant</span><span class="p">,</span>
                             <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dQuant</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DenseQuant</span><span class="p">))</span> <span class="ow">and</span> <span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;LEARNED_SCALE&quot;</span><span class="p">:</span>
            <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="s1">&#39;gamma&#39;</span><span class="p">):</span>
                <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">/</span>
                                <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">+</span> <span class="mf">1e-5</span><span class="p">))</span>
                <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell_weight_para</span> <span class="o">*</span> <span class="n">scale_factor</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">per_channel</span><span class="p">:</span>
                <span class="n">max_init</span> <span class="o">=</span> <span class="p">[</span><span class="n">compute_kl_threshold</span><span class="p">(</span><span class="n">weight_para_each</span><span class="p">,</span> <span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">quant_dtype</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">weight_para_each</span> <span class="ow">in</span> <span class="n">subcell_weight_para</span><span class="p">]</span>
                <span class="n">min_init</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">max_init</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">max_init</span> <span class="o">=</span> <span class="p">[</span><span class="n">compute_kl_threshold</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">quant_dtype</span><span class="p">)]</span>
                <span class="n">min_init</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">max_init</span><span class="p">]</span>

            <span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">reset</span><span class="p">(</span><span class="n">quant_dtype</span><span class="o">=</span><span class="n">cell</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">quant_dtype</span><span class="p">,</span>
                                         <span class="n">min_init</span><span class="o">=</span><span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span><span class="o">=</span><span class="n">max_init</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>